package lesson04;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class TestMain6 {
    public static void main(String[] args) {

        Set<String> set1 = new HashSet<>();

        set1.add("中国");
        set1.add("美国");
        set1.add("日本");
        set1.add("印度");
        set1.add("印度"); // 去重了
        System.out.println(set1.size());
        set1.forEach(System.out::println);
        //
        System.out.println("------------------------------");
        Set<Student> set2 = new HashSet<>();
        set2.add(new Student("Tom", 1));
        set2.add(new Student("jerry", 2));
        Student stu1 = new Student("mario", 3);
        Student stu2 = new Student("mario", 3);
        set2.add(stu1);
        set2.add(stu2);

        System.out.println(stu1.hashCode());
        System.out.println(stu2.hashCode());
        System.out.println(stu2.equals(stu1));

        System.out.println(set2.size()); // 4
        set2.forEach(System.out::println);

    }
}
class Student implements Comparable<Student>{
    private String name;
    private int sno;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return sno == student.sno &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, sno);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sno=" + sno +
                '}';
    }

    public Student() {
    }

    public Student(String name, int sno) {
        this.name = name;
        this.sno = sno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    @Override
    public int compareTo(Student o) {
        // 学号的升序
        //  当前 - 后面 就是升序
        //  后面 - 当前  就是降序
        return -(this.sno - o.sno);
    }
}

